package com.hairline.service;

import com.hairline.bean.*;
import com.hairline.bean.vo.WxAddressDetailVO;
import com.hairline.bean.vo.WxAddressRegionVO;
import com.hairline.bean.vo.WxAddressVO;
import com.hairline.mapper.AddressMapper;
import com.hairline.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<WxAddressVO> addressList(int userId) {
        List<WxAddressVO> result = new ArrayList<>();
        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause("id");
        addressExample.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        //取出初步结果
        for (Address address : addressList) {
            //根据地址id获得id list
            List<Region> regions = selectRegion(address);
            //对地址进行字符串拼接
            StringBuilder sb = new StringBuilder();
            for (Region region : regions) {
                sb.append(region.getName());
            }
            String completeAddress = sb.append(" ").append(address.getAddress()).toString();
            WxAddressVO wxAddressVO = new WxAddressVO(
                    address.getIsDefault(),
                    completeAddress,
                    address.getName(),
                    address.getMobile(),
                    address.getId()
            );
            result.add(wxAddressVO);
        }
        return result;
    }

    @Override
    public WxAddressDetailVO addressDetail(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        //根据地址id获得id list
        List<Region> regions = selectRegion(address);
        WxAddressDetailVO wxAddressDetailVO = new WxAddressDetailVO(
                address.getIsDefault(),
                address.getAreaId(),
                address.getAddress(),
                regions.get(1).getName(),
                regions.get(2).getName(),
                address.getName(),
                address.getMobile(),
                address.getId(),
                address.getCityId(),
                regions.get(0).getName(),
                address.getProvinceId()
        );
        System.out.println(wxAddressDetailVO);
        return wxAddressDetailVO;
    }

    @Override
    public List<WxAddressRegionVO> regionList(Integer pid) {
        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andPidEqualTo(pid);
        List<Region> regions = regionMapper.selectByExample(regionExample);
        List<WxAddressRegionVO> result = new ArrayList<>();
        for (Region region : regions) {
            WxAddressRegionVO wxAddressRegionVO = new WxAddressRegionVO(
                    region.getId(),
                    region.getPid(),
                    region.getName(),
                    region.getType(),
                    region.getCode()
            );
            result.add(wxAddressRegionVO);
        }
        return result;
    }

    @Override
    public int addressSave(Address address) {
        Integer addressId = address.getId();
        //数据库中已存在该地址，需要进行更新
        address.setUpdateTime(new Date());
        address.setDeleted(false);
        if (addressId != 0) {
            addressMapper.updateByPrimaryKeySelective(address);
        } else {
            //数据库中不存在该地址，需要新增
            address.setAddTime(new Date());
            addressMapper.insert(address);
            addressId = addressMapper.selectLastInsertId();
        }
        //如果设置该地址为默认权限，将该用户其他地址设为非默认
        if (address.getIsDefault()) {
            Integer userId = address.getUserId();
            AddressExample addressExample = new AddressExample();
            addressExample.createCriteria().andUserIdEqualTo(userId).andIdNotEqualTo(addressId);
            Address noDefaultAddress = new Address();
            noDefaultAddress.setIsDefault(false);
            addressMapper.updateByExampleSelective(noDefaultAddress, addressExample);
        }

        return addressId;
    }

    @Override
    public void addressDelete(Integer id) {
        Address address = new Address();
        address.setId(id);
        address.setDeleted(true);
        addressMapper.updateByPrimaryKeySelective(address);
    }

    private List<Region> selectRegion(Address address) {
//        List<Integer> addressIds = new ArrayList<>();
//        addressIds.add(address.getProvinceId());
//        addressIds.add(address.getCityId());
//        addressIds.add(address.getAreaId());
        //根据id list查询对应地址
//        RegionExample regionExample = new RegionExample();
        //添加排序
//        regionExample.setOrderByClause("id");
//        regionExample.createCriteria().andIdIn(addressIds);

        Integer provinceId = address.getProvinceId();
        RegionExample regionExample1 = new RegionExample();
        regionExample1.createCriteria().andIdEqualTo(provinceId).andTypeEqualTo((byte) 1);
        List<Region> region1 = regionMapper.selectByExample(regionExample1);
        Integer cityId = address.getCityId();
        RegionExample regionExample2 = new RegionExample();
        regionExample2.createCriteria().andIdEqualTo(cityId).andTypeEqualTo((byte) 2);
        List<Region> region2 = regionMapper.selectByExample(regionExample2);
        Integer areaId = address.getAreaId();
        RegionExample regionExample3 = new RegionExample();
        regionExample3.createCriteria().andIdEqualTo(areaId).andTypeEqualTo((byte) 3);
        List<Region> region3 = regionMapper.selectByExample(regionExample3);
        List<Region> regions = new ArrayList<>();
        regions.add(region1.get(0));
        regions.add(region2.get(0));
        regions.add(region3.get(0));
        return regions;
    }

}
